﻿Public Class FormSearch
	Public prevForm As Form

	Public Sub SearchAll(name As String)
		If name <> "" Then
			ModuleControls.GenerateListViewSearch(ListViewSearchAll)
			ModuleControls.GenerateListViewSearch(ListViewSearchAlbums)
			ModuleControls.GenerateListViewSearch(ListViewSearchArtists)
			ModuleControls.GenerateListViewSearch(ListViewSearchTracks)

			TextBoxSearch.Text = name

			SearchAlbum()
			SearchArtist()
			SearchTrack()
		End If
	End Sub

	Private Sub SearchAlbum()
		Dim methodAlbumSearch = New AlbumSearch(TextBoxSearch.Text, NumericUpDownLimit.Value.ToString, NumericUpDownPage.Value.ToString)
		methodAlbumSearch.sendRequest()

		Dim responseAlbum = methodAlbumSearch.getResponse()

		If IsNothing(responseAlbum) Then
			Return
		End If

		ModuleAlbum.AddToListView(responseAlbum.albums, ListViewSearchAll)
		ModuleAlbum.AddToListView(responseAlbum.albums, ListViewSearchAlbums)
	End Sub

	Private Sub SearchArtist()
		Dim methodArtistSearch = New ArtistSearch(TextBoxSearch.Text, NumericUpDownLimit.Value.ToString, NumericUpDownPage.Value.ToString)
		methodArtistSearch.sendRequest()

		Dim responseArtist = methodArtistSearch.getResponse()

		If IsNothing(responseArtist) Then
			Return
		End If

		ModuleArtist.AddToListView(responseArtist.artists, ListViewSearchAll)
		ModuleArtist.AddToListView(responseArtist.artists, ListViewSearchArtists)
	End Sub

	Private Sub SearchTrack()
		Dim methodTrackSearch = New TrackSearch(TextBoxSearch.Text, "", NumericUpDownLimit.Value.ToString, NumericUpDownPage.Value.ToString)
		methodTrackSearch.sendRequest()

		Dim responseTrack = methodTrackSearch.getResponse()

		If IsNothing(responseTrack) Then
			Return
		End If

		ModuleTrack.AddToListView(responseTrack.tracks, ListViewSearchAll)
		ModuleTrack.AddToListView(responseTrack.tracks, ListViewSearchTracks)
	End Sub

	Private Sub ButtonSearch_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSearch.Click
		ModuleControls.GenerateListViewSearch(ListViewSearchAll)
		ModuleControls.GenerateListViewSearch(ListViewSearchAlbums)
		ModuleControls.GenerateListViewSearch(ListViewSearchArtists)
		ModuleControls.GenerateListViewSearch(ListViewSearchTracks)

		ApplicationMain.AddAutoComplete(TextBoxSearch.Text)

		SearchAlbum()
		SearchArtist()
		SearchTrack()

	End Sub

	Private Sub ListViewSearchAll_ItemActivate(sender As Object, e As System.EventArgs) Handles ListViewSearchAll.ItemActivate
		If ListViewSearchAll.SelectedItems.Count > 0 Then
			If ListViewSearchAll.Groups.Count > 0 Then
				Select Case ListViewSearchAll.SelectedItems.Item(0).Group.ToString
					Case "Albums"
						Dim test = FormAlbumDisplay.LoadAlbum(ListViewSearchAll.SelectedItems.Item(0).SubItems("Album Name").Text, ListViewSearchAll.SelectedItems.Item(0).SubItems("Artist Name").Text, ListViewSearchAll.SelectedItems.Item(0).SubItems("MBID").Text)
						If test = True Then
							FormAlbumDisplay.prevForm = Me
							FormAlbumDisplay.Show()
							Me.Hide()
						End If
					Case "Artists"
						Dim test = FormArtistDisplay.LoadArtist(ListViewSearchAll.SelectedItems.Item(0).SubItems("Artist Name").Text, ListViewSearchAll.SelectedItems.Item(0).SubItems("MBID").Text)
						If test = True Then
							FormArtistDisplay.prevForm = Me
							FormArtistDisplay.Show()
							Me.Hide()
						End If
					Case "Tracks"
						'Dim test = FormTrackDisplay.LoadAm(ListViewSearchAll.SelectedItems.Item(0).SubItems("Track Name").Text, ListViewSearchAll.SelectedItems.Item(0).SubItems("Artist Name").Text, ListViewSearchAll.SelectedItems.Item(0).SubItems("MBID").Text)
						'If test = True Then
						' FormAlbumDisplay.prevForm = Me
						'FormAlbumDisplay.Show()
						'Me.Hide()
						'End If
					Case Else
				End Select
			End If
		End If
	End Sub

	Private Sub ListViewSearchAlbums_ItemActivate(sender As Object, e As System.EventArgs) Handles ListViewSearchAlbums.ItemActivate
		If ListViewSearchAlbums.SelectedItems.Count > 0 Then
			Dim test = FormAlbumDisplay.LoadAlbum(ListViewSearchAlbums.SelectedItems.Item(0).SubItems("Album Name").Text, ListViewSearchAlbums.SelectedItems.Item(0).SubItems("Artist Name").Text, ListViewSearchAlbums.SelectedItems.Item(0).SubItems("MBID").Text)
			If test = True Then
				FormAlbumDisplay.prevForm = Me
				FormAlbumDisplay.Show()
				Me.Hide()
			End If
		End If
	End Sub

	Private Sub ListViewSearchArtists_ItemActivate(sender As Object, e As System.EventArgs) Handles ListViewSearchArtists.ItemActivate
		If ListViewSearchArtists.SelectedItems.Count > 0 Then
			Dim test = FormArtistDisplay.LoadArtist(ListViewSearchArtists.SelectedItems.Item(0).SubItems("Artist Name").Text, ListViewSearchArtists.SelectedItems.Item(0).SubItems("MBID").Text)
			If test = True Then
				FormArtistDisplay.prevForm = Me
				FormArtistDisplay.Show()
				Me.Hide()
			End If
		End If
	End Sub

	Private Sub FormAlbumSearch_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If Not IsNothing(prevForm) Then
			prevForm.Show()
			prevForm = Nothing
		End If
	End Sub

	Private Sub ButtonGoBack_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGoBack.Click
		Me.Close()
	End Sub

	Private Sub FormAlbumSearch_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		If ComboBoxView.SelectedItem = "" Then
			ComboBoxView.SelectedItem = "Large Icon"
		End If

		TextBoxSearch.AutoCompleteCustomSource = ApplicationMain.autoCompleteCollection

		If My.Settings.auto_complete Then
			TextBoxSearch.AutoCompleteMode = AutoCompleteMode.Suggest
		Else
			TextBoxSearch.AutoCompleteMode = AutoCompleteMode.None
		End If

		TagFilterBox.CharacterCasing() = CharacterCasing.Lower

	End Sub

	Private Sub ComboBoxView_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBoxView.SelectedIndexChanged
		Select Case ComboBoxView.SelectedIndex
			Case 0
				ListViewSearchAll.View() = View.LargeIcon
				ListViewSearchAlbums.View() = View.LargeIcon
				ListViewSearchArtists.View() = View.LargeIcon
				ListViewSearchTracks.View() = View.LargeIcon
			Case 1
				ListViewSearchAll.View() = View.Details
				ListViewSearchAlbums.View() = View.Details
				ListViewSearchArtists.View() = View.Details
				ListViewSearchTracks.View() = View.Details
			Case 2
				ListViewSearchAll.View() = View.SmallIcon
				ListViewSearchAlbums.View() = View.SmallIcon
				ListViewSearchArtists.View() = View.SmallIcon
				ListViewSearchTracks.View() = View.SmallIcon
			Case 3
				ListViewSearchAll.View() = View.List
				ListViewSearchAlbums.View() = View.List
				ListViewSearchArtists.View() = View.List
				ListViewSearchTracks.View() = View.List
			Case 4
				ListViewSearchAll.View() = View.Tile
				ListViewSearchAlbums.View() = View.Tile
				ListViewSearchArtists.View() = View.Tile
				ListViewSearchTracks.View() = View.Tile

		End Select
	End Sub


	Private Sub TagFilterButton_Click(sender As System.Object, e As System.EventArgs) Handles TagFilterButton.Click

		If (TagFilterBox.Text.Length = 0) Then	  'lets not waste time on empty searches
			Return
		End If

		ModuleControls.GenerateListViewSearch(ListViewSearchAll)
		ModuleControls.GenerateListViewSearch(ListViewSearchAlbums)
		ModuleControls.GenerateListViewSearch(ListViewSearchArtists)
		ModuleControls.GenerateListViewSearch(ListViewSearchTracks)

		ApplicationMain.AddAutoComplete(TagFilterBox.Text)

		FilteredSearchAlbum()
		FilteredSearchArtist()
		FilteredSearchTrack()

	End Sub

	Private Sub FilteredSearchAlbum()

		Dim methodAlbumSearch = New AlbumSearch(TextBoxSearch.Text, (NumericUpDownLimit.Value * 1).ToString, NumericUpDownPage.Value.ToString)
		methodAlbumSearch.sendRequest()

		Dim responseAlbum = methodAlbumSearch.getResponse()
		Dim albumTags As AlbumGetTopTagsResponse
		Dim filteredAlbums As AlbumSearchResponse
		ReDim filteredAlbums.albums(NumericUpDownLimit.Value - 1)
		Dim index As Integer = 0
		If IsNothing(responseAlbum) Then
			Return
		End If

		For i As Integer = 0 To NumericUpDownLimit.Value * 1
			If (i >= responseAlbum.albums.Count()) Then
				Exit For
			End If
			Dim albumTagRequest = New AlbumGetTopTags(responseAlbum.albums(i).artist, responseAlbum.albums(i).name, responseAlbum.albums(i).mbid, False)
			albumTagRequest.sendRequest()
			albumTags = albumTagRequest.getResponse()

			If (albumTagRequest.xmlresponse.<lfm>.<toptags>.<tag>.Count() = 0) Then
				'If Not (VarType(albumTags) > vbArray) Then
				Continue For
			End If

			For j As Integer = 0 To albumTags.tags.Count - 1
				If albumTags.tags(j).name = TagFilterBox.Text Then
					filteredAlbums.albums(index) = responseAlbum.albums(i)
					index += 1
					Exit For
				End If
			Next
		Next

		Dim finalResult As AlbumSearchResponse
		ReDim finalResult.albums(index - 1)
		For i As Integer = 0 To index - 1
			finalResult.albums(i) = filteredAlbums.albums(i)
		Next

		'responseAlbum = finalResult  '<--by doing this the filter tags are cumulative.  
		'comment this out to have it apply the filter to all originally searched albums

		ModuleAlbum.AddToListView(finalResult.albums, ListViewSearchAll)
		ModuleAlbum.AddToListView(finalResult.albums, ListViewSearchAlbums)
	End Sub

	Private Sub FilteredSearchArtist()

		Dim methodArtistSearch = New ArtistSearch(TextBoxSearch.Text, (NumericUpDownLimit.Value * 1).ToString, NumericUpDownPage.Value.ToString)
		methodArtistSearch.sendRequest()

		Dim responseArtist = methodArtistSearch.getResponse()
		Dim artistTags As ArtistGetTopTagsResponse
		Dim filteredArtists As ArtistSearchResponse
		ReDim filteredArtists.artists(NumericUpDownLimit.Value - 1)
		Dim index As Integer = 0
		If IsNothing(responseArtist) Then
			Return
		End If

		For i As Integer = 0 To NumericUpDownLimit.Value * 1
			If (i >= filteredArtists.artists.Count()) Then
				Exit For
			End If
			Dim artistTagRequest = New ArtistGetTopTags(responseArtist.artists(i).name, responseArtist.artists(i).mbid, False)
			artistTagRequest.sendRequest()
			artistTags = artistTagRequest.getResponse()

			If (artistTagRequest.xmlresponse.<lfm>.<toptags>.<tag>.Count() = 0) Then
				'If Not (VarType(artistTags) > vbArray) Then
				Continue For
			End If

			For j As Integer = 0 To artistTags.tags.Count - 1
				If artistTags.tags(j).name = TagFilterBox.Text Then
					filteredArtists.artists(index) = responseArtist.artists(i)
					index += 1
					Exit For
				End If
			Next
		Next

		Dim finalResult As ArtistSearchResponse
		ReDim finalResult.artists(index - 1)
		For i As Integer = 0 To index - 1
			finalResult.artists(i) = filteredArtists.artists(i)
		Next

		'responseArtist = finalResult  '<--by doing this the filter tags are cumulative.  
		'comment this out to have it apply the filter to all originally searched artists

		ModuleArtist.AddToListView(finalResult.artists, ListViewSearchAll)
		ModuleArtist.AddToListView(finalResult.artists, ListViewSearchArtists)
	End Sub

	Private Sub FilteredSearchTrack()
		Dim methodtracksearch = New TrackSearch(TextBoxSearch.Text, "", (NumericUpDownLimit.Value * 1).ToString, NumericUpDownPage.Value.ToString)
		methodtracksearch.sendRequest()

		Dim responsetrack = methodtracksearch.getResponse()
		Dim trackTags As TrackGetTopTagsResponse
		Dim filteredtracks As TrackSearchResponse
		ReDim filteredtracks.tracks(NumericUpDownLimit.Value - 1)
		Dim index As Integer = 0
		If IsNothing(responsetrack) Then
			Return
		End If

		For i As Integer = 0 To NumericUpDownLimit.Value * 1
			If (i >= filteredtracks.tracks.Count()) Then
				Exit For
			End If
			Dim trackTagRequest = New TrackGetTopTags(responsetrack.tracks(i).name, responsetrack.tracks(i).artist, responsetrack.tracks(i).mbid, False)
			trackTagRequest.sendRequest()
			trackTags = trackTagRequest.getResponse()

			If (trackTagRequest.xmlresponse.<lfm>.<toptags>.<tag>.Count() = 0) Then
				'If Not (VarType(trackTags) > vbArray) Then
				Continue For
			End If

			For j As Integer = 0 To trackTags.tags.Count - 1
				If trackTags.tags(j).name = TagFilterBox.Text Then
					filteredtracks.tracks(index) = responsetrack.tracks(i)
					index += 1
					Exit For
				End If
			Next
		Next

		Dim finalResult As TrackSearchResponse
		ReDim finalResult.tracks(index - 1)
		For i As Integer = 0 To index - 1
			finalResult.tracks(i) = filteredtracks.tracks(i)
		Next

		'responsetrack = finalResult  '<--by doing this the filter tags are cumulative.  
		'comment this out to have it apply the filter to all originally searched tracks

		ModuleTrack.AddToListView(finalResult.tracks, ListViewSearchAll)
		ModuleTrack.AddToListView(finalResult.tracks, ListViewSearchTracks)
	End Sub

End Class